/*
 * ButtonImage.fx
 *
 * Created on 16-may-2009, 17:01:30
 */

package webooksfx_desktop.view.components;

import javafx.scene.Cursor;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;

/**
 * @author Administrador
 */
public class ButtonImage extends CustomNode {
	public var action: function() : Void;
	public var image: String = "";
	public var image_back: String = "";
	public var width: Integer = 0;
	public var height: Integer = 0;

	var click: Boolean = false;
	var image_view: ImageView = null;

	public var alto: Integer = bind image_view.boundsInLocal.height as Integer;
	public var ancho: Integer = bind image_view.boundsInLocal.width as Integer;

	public function imageError () : Boolean {
		return image_view.image.error
	}

	public override function create(): Node {

		return Group {
			cursor: Cursor.HAND
			content: [
				image_view = ImageView {
					fitWidth: bind width
					fitHeight: bind height
					preserveRatio: true
					image: bind Image {
						url: image
						backgroundLoading: if (image_back != "") true else false;
						placeholder: Image {
							url: image_back
						}
					}
					//==============================================================================
					// MOUSE EVENTS
					//==============================================================================
					onMousePressed: function( e: MouseEvent ):Void {
						click = true;
					}
					onMouseReleased: function( e: MouseEvent ):Void {
						if (click and hover) {
							action();
						}
						click = false;
					}
				}
			]
		};
	}

	public function getHeight() : Integer {
		return alto;
	}
	public function getWidth() : Integer {
		return ancho;
	}
}